﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SuperSurvey.UserControl;
namespace SuperSurvey.GUI
{
    public partial class CreateSurvey : System.Web.UI.Page
    {
        private int count = 1;
        //HttpCookie cookie;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadPostBack();
        }

        private void LoadPostBack()
        {
            if (Session[Session.Count.ToString()] != null)
            {
                for (int i = 1; i <= Session.Count; i++)
                {
                    AddQuestion myQuestion = LoadControl("~/UserControl/AddQuestion.ascx") as AddQuestion;
                    myQuestion.ddlQuetionTypeSelectedIndexChanged += new ddlQuestiontype_SelectedIndexChanged(AddQuestionControl_ddlQuestionTypeChanged);
                    myQuestion.OnimgbtnAddNewAnswerclk += new imgbtnAddNewAnswer_Click(AddNewAnswer_ImgbtnAddNewAnswerClick);
                    //myQuestion.OnBtnSaveSessionQuestionClick += new btnSessionQuestion_Click(SaveSessionQuestion_BtnSaveSessionQuestion);
                    myQuestion.LabelQuestionNumber.Text = "Question Number: " + (i).ToString();
                    pnlContent.Controls.Add(myQuestion);
                }
            }
        }
        string AutoCreateSessionID()
        {
            string id = count.ToString();
            count++;
            return id;
        }
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            AddQuestion myQuestion = LoadControl("~/UserControl/AddQuestion.ascx") as AddQuestion;
            myQuestion.ddlQuetionTypeSelectedIndexChanged += new ddlQuestiontype_SelectedIndexChanged(AddQuestionControl_ddlQuestionTypeChanged);
            myQuestion.OnimgbtnAddNewAnswerclk += new imgbtnAddNewAnswer_Click(AddNewAnswer_ImgbtnAddNewAnswerClick);
            //myQuestion.OnBtnSaveSessionQuestionClick += new btnSessionQuestion_Click(SaveSessionQuestion_BtnSaveSessionQuestion);
            myQuestion.LabelQuestionNumber.Text = "Question Number: " + (Session.Count + 1).ToString();
            pnlContent.Controls.Add(myQuestion);
            Session.Add((Session.Count + 1).ToString(), myQuestion);            
            myQuestion.QuestionTitle.Focus();
        }

        void AddQuestionControl_ddlQuestionTypeChanged(object sender, EventArgs e)
        {
            AddQuestion question = (AddQuestion)(sender);
            if (question.QuestionType.SelectedIndex == 0)
            {
                question.MultiViewAnswer.SetActiveView(question.ViewTextBox);
                question.ImageButtonAddNewItem.Enabled = false;
                question.NumberAnswer.Enabled = false;
                question.QuestionTitle.Focus();
            }
            else if (question.QuestionType.SelectedIndex == 1)
            {
                question.MultiViewAnswer.SetActiveView(question.ViewCheckboxAnswer);
                question.ImageButtonAddNewItem.Enabled = true;
                question.NumberAnswer.Enabled = true;
                question.QuestionTitle.Focus();
            }
            else if (question.QuestionType.SelectedIndex == 2)
            {
                question.MultiViewAnswer.SetActiveView(question.ViewRadioAnswer);
                question.ImageButtonAddNewItem.Enabled = true;
                question.NumberAnswer.Enabled = true;
                question.QuestionTitle.Focus();
            }
            
        }
        void AddNewAnswer_ImgbtnAddNewAnswerClick(object sender, ImageClickEventArgs e)
        {
            AddQuestion question = (AddQuestion)(sender);
            if (question.QuestionType.SelectedIndex == 1)
            {
                int count = Convert.ToInt32(question.NumberAnswer.Text);
                for (int i = 1; i <= count; i++)
                {
                    CheckboxAnswer chkboxAnswer = (CheckboxAnswer)LoadControl("~/UserControl/CheckboxAnswer.ascx");
                    question.PanelCheckBoxAnswer.Controls.Add(chkboxAnswer);
                    Session[Session.Count.ToString() + i.ToString()] = chkboxAnswer;
                    
                }
                question.QuestionTitle.Focus();
                
            }
            else if (question.QuestionType.SelectedIndex == 2)
            {
                int count = Convert.ToInt32(question.NumberAnswer.Text);
                for (int i = 1; i <= count; i++)
                {
                    RadioAnswer radioAnswer = LoadControl("~/UserControl/RadioAnswer.ascx") as RadioAnswer;
                    question.PanelRadioAnswer.Controls.Add(radioAnswer);
                    Session[Session.Count.ToString() + i.ToString()] = radioAnswer;
                    
                }

                question.QuestionTitle.Focus();
            }
        }

        
        //void SaveSessionQuestion_BtnSaveSessionQuestion(object sender, EventArgs e) {
            
        //}

    }
}